<?xml version="1.0"?>

<st-source>
<time-stamp>From VisualWorks® NonCommercial, 7.6 of March 3, 2008 on March 6, 2009 at 4:49:53 pm</time-stamp>


<class>
<name>Random</name>
<environment>Core</environment>
<super>Core.Stream</super>
<private>false</private>
<indexed-type>none</indexed-type>
<inst-vars>seed </inst-vars>
<class-inst-vars>defaultGeneratorClass </class-inst-vars>
<imports></imports>
<category>Magnitude-Numbers</category>
<attributes>
<package>Magnitude-Numbers</package>
</attributes>
</class>

<!-- -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -   -->


<methods>
<class-id>Core.Random class</class-id> <category>class initialization</category>

<body package="Magnitude-Numbers" selector="initialize">initialize
	"Random initialize"

	self defaultGeneratorClass: LaggedFibonacciRandom</body>
</methods>

<methods>
<class-id>Core.Random class</class-id> <category>instance creation</category>

<body package="Magnitude-Numbers" selector="fastest">fastest

	^FastRandom new</body>

<body package="Magnitude-Numbers" selector="fromGenerator:seededWith:">fromGenerator: g seededWith: s 
	"Answer a new random number generator."

	^FastRandom fromGenerator: g seededWith: s</body>

<body package="Magnitude-Numbers" selector="standard">standard

	^LaggedFibonacciRandom new</body>
</methods>

<methods>
<class-id>Core.Random class</class-id> <category>accessing</category>

<body package="Magnitude-Numbers" selector="defaultGeneratorClass">defaultGeneratorClass

	^defaultGeneratorClass</body>

<body package="Magnitude-Numbers" selector="defaultGeneratorClass:">defaultGeneratorClass: aClass
	"Allow customers to change the default random generator"

	(aClass inheritsFrom: self randomNumberGeneratorRootClass) ifFalse:
		[self error: (#ErrorForNumberMustBeRandomSubclass &lt;&lt; #dialogs &gt;&gt; ('The default random number generator must be a subclass of ', self randomNumberGeneratorRootClass name))].
	defaultGeneratorClass := aClass</body>

<body package="Magnitude-Numbers" selector="new">new
	"Answer a new random number generator."

	^self defaultGeneratorClass new</body>
</methods>

<methods>
<class-id>Core.Random class</class-id> <category>private</category>

<body package="Magnitude-Numbers" selector="basicNewInstance">basicNewInstance

	^self basicNew initialize</body>

<body package="Magnitude-Numbers" selector="randomNumberGeneratorRootClass">randomNumberGeneratorRootClass

	^Random</body>
</methods>

<!-- -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -   -->


<comment>
<class-id>Core.Random</class-id>
<body>This is a general starting location for creating different kinds of random number generators.

Instance Variables:

	seed	&lt;Integer | Double&gt;		Each call to #next uses the seed as input to a function which computes a new value for the seed, and also uses the new seed value to answer the next random number.  Different generators may support this functionality to different degrees.

Class Instance Variables:

	defaultGeneratorClass 	&lt;Random class&gt;	class to use for new random generators if the user does not specify which to use.  The default generator class is LaggedFibonacciRandom.</body>
</comment>

<methods>
<class-id>Core.Random</class-id> <category>accessing</category>

<body package="Magnitude-Numbers" selector="seed:">seed: aSmallInteger
	"Initialize the first random number."
	
	seed := aSmallInteger</body>
</methods>

<methods>
<class-id>Core.Random</class-id> <category>testing</category>

<body package="Magnitude-Numbers" selector="atEnd">atEnd
	"Answer false that the stream is not at an end."

	^false</body>

<body package="Magnitude-Numbers" selector="isReadable">isReadable
	"Answer whether the stream implements #next."

	^true</body>

<body package="Magnitude-Numbers" selector="isWritable">isWritable
	"Answer whether the stream implements #nextPut:."

	^false</body>
</methods>

<methods>
<class-id>Core.Random</class-id> <category>stream - should not implement</category>

<body package="Magnitude-Numbers" selector="close">close
	"Random number generator streams should not close"

	^self shouldNotImplement</body>

<body package="Magnitude-Numbers" selector="commit">commit
	"Random number generator streams should not commit"

	^self shouldNotImplement</body>

<body package="Magnitude-Numbers" selector="contents">contents
	"Random number generators do not have a contents
	so provide an error notification."

	^self shouldNotImplement</body>

<body package="Magnitude-Numbers" selector="do:">do: aBlock
	"Since the receiver is never atEnd, this message
	should not be implemented"

	^self shouldNotImplement</body>

<body package="Magnitude-Numbers" selector="flush">flush
	"Random number generators do not need to flush."

	^self shouldNotImplement</body>

<body package="Magnitude-Numbers" selector="nextPut:">nextPut: anObject
	"Random numbers generators do not implement nextPut:
	so provide an error notification."

	^self shouldNotImplement</body>

<body package="Magnitude-Numbers" selector="upToEnd">upToEnd
	"Random number generators are never atEnd.
	Therefore, this message should not be implemented"

	^self shouldNotImplement</body>
</methods>

<methods>
<class-id>Core.Random</class-id> <category>private</category>

<body package="Magnitude-Numbers" selector="initialize">initialize

	^self</body>
</methods>

<methods>
<class-id>Core.Random</class-id> <category>stream - refinement</category>

<body package="SupplyChain" selector="nextInt:">nextInt: maxValue
	"comment stating purpose of message"

	^(self next * maxValue) asInteger</body>

<body package="Magnitude-Numbers" selector="nextValue">nextValue
	"Answer the next random number value, but, unlike the message
	next, do not change the internal state of the generator"

	^self subclassResponsibility</body>

<body package="Magnitude-Numbers" selector="peek">peek

	^self nextValue</body>
</methods>

<initialize>
<class-id>Core.Random</class-id>
</initialize>

</st-source>
